"""
A class to exercise the JT core functions.
"""
import time

import itk
import vtk

import connect2
import ImageCompare

import JT
import Calibration

import StandAlone_GetMetric2 as itkfx

def Test():
    """
    Test out the ITK registration procedure.
    """

    inputVolFileName = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.mha'
    staFile = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.sta'
    calFileLat = 'C:/Users/bryan/bryan-code/trunk/TestData/ext_cal1.txt'
    #drrImage = None

    # These should be normalized images
    fixedImageName = 'C:/Users/bryan/bryan-code/2D3D/vert1/fluoro/ushortim080-LAT.mhd'
    outPath = 'C:/Users/bryan/bryan-code/trunk/Images/'

    #
    #  ITK Image Components
    #%#####################
    itk_reader = itk.ImageFileReader.IF2.New()
    itk_reader.SetFileName(fixedImageName)
    itk_reader.Update()

    #
    # Create Lateral DRR
    #%###########################
    xraycamLat = JT.XRayCamera()
    drrLat = JT.DRR()
    calLat = Calibration.ExternalCalibration()

    calLat.LoadConsolidatedCalibration(calFileLat)
    calLat.LoadStaFile(staFile)
    drrLat.SetXRayCamera(xraycamLat)
    drrLat.SetVolumeFilename(inputVolFileName)
    drrLat.SetBlackOnWhiteImage()

    #
    # Automatic calibration

    #%##########################
    xraycamLat.SetExternalCalibration(calLat)

    vtkMatrixLAT = calLat.ConvertNumpyToVTKArray(calLat._VolumeT)
    drrLat.SetVolumeUserMatrix(vtkMatrixLAT)
    drrLat.GenerateImage()

##    # Set the DRR projector to the registration
##    # Create Registraion object
##    reg = JT.Registration()
##    reg.SetDRR(drrLat)
##    reg.SetFixedImage(itk_reader.GetOutput())
##    reg.SetupITKRegistration()
##    reg.StartRegistration()

    #setup a VTK writer for debugging
    writer = itk.ImageFileWriter.IUC2.New()
    caster = itk.CastImageFilter.IF2IUC2.New()

##    writer.SetFileName(outPath+'CalibratedDRRImage.mhd')
##    writer.SetInput(drrLat.GetImage('itk'))
##    writer.Write()

    movingImagePath=outPath+'CurrentMovingImage.mhd'

    #reg=itkfx.Reg()

    writer.SetFileName(movingImagePath)

    reg = itkfx.reg()
    #reg=JT.NewRegistration()
    reg.SetFixedFileName(fixedImageName)
    #reg._DRR = drrLat
    reg.SetMovingFileName(movingImagePath)


    for i in range(20):

        #drrLat._volume.SetOpacityThreshold(dispSetting)
        drrLat._volume.RotateWXYZ(1,0,0,1)
        drrLat._renWin.Render()
        start=time.clock()
        drrLat.GenerateImage()
        end1=time.clock()-start
##        writer.SetInput(drrLat.GetImage('itk'))
##        writer.Write()

        caster.SetInput(drrLat.GetImage('itk'))
        writer.SetInput(caster.GetOutput())
        writer.SetFileName(outPath+('CurrentMovingImage_%s.png'%str(i)))
        writer.Write()

        reg.SetMovingFileName(outPath+('CurrentMovingImage_%s.png'%str(i)))
        value = reg.GetMetric()
        end2=time.clock()-start
        print ("CurrentMovingImage_%s.png: i: %i, Metric Value: %f,\
                DRR time (s): %f, Metric time (s): %f") % (str(i),i, value,end1,end2)


if __name__ == "__main__":
    Test()